<xsl:stylesheet version="1.1"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:our="http://www.ouroboroswiki.com/ext">

  <xsl:output method="xml" indent="yes" />

  <xsl:param name="wiki-base" />
  <xsl:param name="input-mime-type">text/plain</xsl:param>
  <xsl:param name="input-path"/>
  <xsl:param name="input-repo"/>
  <xsl:param name="main-path"/>
  <xsl:param name="main-repo"/>
  <xsl:param name="input-editable">false</xsl:param>
  <xsl:param name="original-pipe"><xsl:value-of select="$input-repo"/></xsl:param>

  <xsl:template match="/">
    <div>
       <xsl:if test="$input-editable = 'false'">
         <xsl:text>Warning: this file does not appear to be editable</xsl:text><br/>
       </xsl:if>
       <our:form action="save" path="{$original-pipe}" mode="absolute">
         <xsl:if test="$main-path">
           <our:parameter name="path"><xsl:value-of select="$main-path"/></our:parameter>
         </xsl:if>
         <xsl:if test="$main-repo">
           <our:parameter name="repo"><xsl:value-of select="$main-repo"/></our:parameter>
         </xsl:if>
         <textarea id="content" name="content" rows="40" cols="200">
           <xsl:choose>
             <xsl:when test="string-length(content/escaped)>0">
            <xsl:value-of select="content/escaped" />
             </xsl:when>
             <xsl:otherwise>
               <xsl:choose>
                 <xsl:when test="contains($input-mime-type, '/html') or contains($input-mime-type, '/xml')">
                   <xsl:text>&lt;div&gt;&lt;!-- new file --&gt;&lt;/div&gt;</xsl:text>
                 </xsl:when>
                 <xsl:otherwise>
                 <xsl:text>New File</xsl:text>
                 </xsl:otherwise>
               </xsl:choose>
             </xsl:otherwise>
           </xsl:choose>
         </textarea>
         <!-- TODO make links -->
         <xsl:choose>
           <xsl:when test="$input-mime-type = 'text/html'">
             <script type="text/javascript">
          CKEDITOR.replace( 'content',{ 
          				filebrowserImageBrowseUrl : '<our:url id="file-browser-images" path="(jpg|gif|svg|png)" repo="images.browse.repo.view"/>',
          				filebrowserBrowseUrl : '<our:url id="file-browser-images" path="xml" repo="browse.repo.view"/>',
						/*
						 * Font face
						 */
						// Define the way font elements will be applied to the document. The "font"
						// element will be used.
						font_style :
						{
								element		: 'font',
								attributes		: { 'face' : '#(family)' }
						},

						/*
						 * Font sizes.
						 */
						fontSize_sizes : 'xx-small/1;x-small/2;small/3;medium/4;large/5;x-large/6;xx-large/7',
						fontSize_style :
							{
								element		: 'font',
								attributes	: { 'size' : '#(size)' }
							} ,

						/*
						 * Font colors.
						 */
						colorButton_enableMore : true,

						colorButton_foreStyle :
							{
								element : 'font',
								attributes : { 'color' : '#(color)' },
								overrides	: [ { element : 'span', attributes : { 'class' : /^FontColor(?:1|2|3)$/ } } ]
							},

						colorButton_backStyle :
							{
								element : 'font',
								styles	: { 'background-color' : '#(color)' }
							}
				});
          
        </script>
           </xsl:when>
           <xsl:otherwise>
             <script type="text/javascript">
               var editor = CodeMirror.fromTextArea('content', {
               height: "350px",
               parserfile: "parsexml.js",
               stylesheet: "<xsl:value-of select="$wiki-base" />/css/codemirror/xmlcolors.css",
               path: "<xsl:value-of select="$wiki-base" />/js/codemirror/",
               continuousScanning: 500,
               lineNumbers: true
               });
             </script>
          </xsl:otherwise>
         </xsl:choose>
         <input type="submit" value="Submit"/>
       </our:form>
       <hr/>
       <our:link id="view-link" repo="view" mode="replace">view</our:link>
       <br/>
       <our:include id="view" repo="view" mode="replace"/>
    </div>
  </xsl:template>

</xsl:stylesheet>